// pages/cart/cart.js
import storage from "../../utils/storage.js"

import request from "../../utils/request.js"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        carts: [],
        allPrice: 0,
        allNum: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let carts = storage.get("carts");
        this.setData({
            carts: carts
        })
        this.getNumPrice();
    },


    handleClick(e) {
        let carts = storage.get("carts")
        let { operation } = e.currentTarget.dataset;
        let { id } = e.currentTarget.dataset;
        // console.log(operation)
        // console.log(id)
        // let allPrice = 0;
        carts.forEach((item, index) => {
            if (item._id === id) {
                if (item.num === 1 && operation === -1) {
                    wx.showModal({
                        title: '删除提示',
                        content: '是否删除此条数据',
                        confirmText: "确定删除",
                        cancelText: "再想想",
                        success: (res) => {
                            if (res.confirm) {
                                carts.splice(index, 1);
                                this.setData({
                                    carts: carts
                                })
                                storage.set("carts", carts)
                            } if (res.cancel) {
                                wx.showToast({
                                    title: '取消删除',
                                })
                            }
                        }
                    })
                } else {
                    item.num += operation
                }
            }
        })
        let allPrice = 0;
        let allNum = 0;
        carts.forEach((item) => {
            allPrice += ((item.price * 100) * item.num) / 100
            allNum += item.num
        })

        this.setData({
            carts: carts,
            allPrice: allPrice,
            allNum: allNum
        })
        storage.set("carts", carts)
    },


    getNumPrice() {
        let carts = storage.get("carts")
        let allPrice = 0;
        let allNum = 0;
        carts.forEach((item) => {
            allPrice += ((item.price * 100) * item.num) / 100
            allNum += item.num
        })
        this.setData({
            carts: carts,
            allPrice: allPrice,
            allNum: allNum
        })
        storage.set("carts", carts)
    },


    // 继续添加
    async addCarts(){
        let {result} = await this.scanCodePromise();
        if(!result){
            return
        }
        let res = await request('/api/getProduct', {qcode: result});
        if(res.success === true && res.result.length > 0){
            this.addCart(res.result[0]);
            wx.reLaunch({
                url: '/pages/cart/cart',
              })
        }
    },

    addCart(data){
        let carts = storage.get("carts") || [];

        let index = carts.findIndex((item, index) => {
            return item._id == data._id
        })
        // console.log(index)

        if(index === -1){
            data.num = 1;
            carts.push(data)
            storage.set("carts", carts)
        }else{
            let item = carts.find((item) => {
                if(item._id === data._id){
                    return item
                }   
            })
            item.num++;
            carts.forEach(i => {
                if(i._id === item._id){
                    i = item[0]
                }
            })
            storage.set("carts", carts)
        }
    },
 
    scanCodePromise(){
        return new Promise((resolve, reject) => {
            wx.scanCode({
                success: (res) => {
                    resolve(res)
                },
                fail: (err) => {
                    reject(err)
                },
              })
        })
    },

    goToOrder(){
        wx.navigateTo({
          url: '/pages/order/order',
        })
    },




    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})